package com.jxwifi.cloud.quickclean.adapter;

import android.graphics.Color;
import android.support.annotation.LayoutRes;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.app.StateUtils;
import com.jxwifi.cloud.quickclean.bean.CardRollBean;
import com.jxwifi.cloud.quickclean.bean.MyCleanersBean;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import static com.google.gson.internal.bind.util.ISO8601Utils.format;

public class CardRollAdapter extends BaseQuickAdapter<CardRollBean, BaseViewHolder> {

    private String mServiceCode;
    private String mMoney;

    public CardRollAdapter(@LayoutRes int layoutResId,String mServiceCode,String mMoney) {
        super(layoutResId, null);
        this.mServiceCode=mServiceCode;
        this.mMoney=mMoney;

    }

    @Override
    protected void convert(BaseViewHolder helper, CardRollBean item) {
        //判断item中的开始与结束时间来显示卡卷的使用
        if (CardTiem(item.getStartTime(), item.getEndTime())) {
            if (!TextUtils.isEmpty(mServiceCode)&&!TextUtils.isEmpty(mMoney)){
                String[] SCode=item.getCoupon().getServiceCodes().split(",");
                boolean mIsSCode=false;
                for (int i=0;i<SCode.length;i++){
                    if (mServiceCode.equals(SCode[i])){
                        mIsSCode=true;
                        break;
                    }
                }
                if (mIsSCode&&Double.valueOf(mMoney)>(double)item.getCoupon().getMin()){
                    helper.getView(R.id.rel_card_roll_bg).setVisibility(View.VISIBLE);
                    CarShow(helper,item);
                }else{
                    //Cardissem(helper,item);
                    helper.getView(R.id.rel_card_roll_bg).setVisibility(View.GONE);
                }
            }else{
                helper.getView(R.id.rel_card_roll_bg).setVisibility(View.VISIBLE);
                CarShow(helper,item);
            }
        } else {
            if (StateUtils.READY.equals(item.getStatus())) {
                //已过期的优惠券不显示
                helper.getView(R.id.rel_card_roll_bg).setVisibility(View.GONE);
            } else {
                helper.getView(R.id.rel_card_roll_bg).setVisibility(View.VISIBLE);
                Cardissem(helper,item);
            }


        }
    }

    public void CarShow(BaseViewHolder helper, CardRollBean item){
        helper.getView(R.id.rel_card_roll_bg).setBackgroundResource(R.mipmap.icon_card_roll_bg);
        helper.setText(R.id.tv_card_roll_price, "\u00A5" + item.getCoupon().getDiscount());
        if (!TextUtils.isEmpty(item.getCoupon().getName())) {
            helper.setText(R.id.tv_card_roll_discount_name, item.getCoupon().getName()).setTextColor(R.id.tv_card_roll_name, Color.parseColor("#333333"));
        } else {
            helper.setText(R.id.tv_card_roll_discount_name, "优惠券").setTextColor(R.id.tv_card_roll_name, Color.parseColor("#333333"));
        }
        helper.setText(R.id.tv_card_roll_name, item.getCouponName());
        String[] getEndTime = item.getEndTime().split(" ");
        helper.setText(R.id.tv_card_roll_item_time, "有效期至：" + getEndTime[0]).setTextColor(R.id.tv_card_roll_item_time, Color.parseColor("#999999"));
        helper.setText(R.id.tv_card_roll_item_city, "使用地区：" + item.getCoupon().getAreaNames()).setTextColor(R.id.tv_card_roll_item_city, Color.parseColor("#999999"));
        helper.setText(R.id.tv_card_roll_use, "去使用");
        helper.getView(R.id.tv_card_roll_use).setBackgroundResource(R.drawable.shap_card_roll_title);
        if (item.getCoupon().getMin() == 0) {
            helper.setText(R.id.tv_card_roll_rule, "使用规则：不限").setTextColor(R.id.tv_card_roll_rule, Color.parseColor("#999999"));
        } else {
            helper.setText(R.id.tv_card_roll_rule, "使用规则：满" + item.getCoupon().getMin() + "元可用").setTextColor(R.id.tv_card_roll_rule, Color.parseColor("#999999"));
        }
        helper.addOnClickListener(R.id.tv_card_roll_use);
    }

    public void Cardissem(BaseViewHolder helper, CardRollBean item){
        helper.getView(R.id.rel_card_roll_bg).setBackgroundResource(R.mipmap.icon_card_roll_bg_nu);
        /*if (StateUtils.READY.equals(item.getStatus())) {
            helper.setText(R.id.tv_card_roll_use, "已过期");
        } else {*/
            helper.setText(R.id.tv_card_roll_use, "已使用");
        //}
        helper.setText(R.id.tv_card_roll_price, "\u00A5" + item.getCoupon().getDiscount());
        helper.getView(R.id.tv_card_roll_use).setBackgroundResource(R.drawable.shap_card_roll_title_un);
        if (!TextUtils.isEmpty(item.getCoupon().getName())) {
            helper.setText(R.id.tv_card_roll_discount_name, item.getCoupon().getName()).setTextColor(R.id.tv_card_roll_name, Color.parseColor("#FFFFFF"));
        } else {
            helper.setText(R.id.tv_card_roll_discount_name, "优惠券").setTextColor(R.id.tv_card_roll_name, Color.parseColor("#FFFFFF"));
        }
        helper.setText(R.id.tv_card_roll_name, item.getCouponName());
        String[] getEndTime = item.getEndTime().split(" ");
        helper.setText(R.id.tv_card_roll_item_time, "有效期至：" + getEndTime[0]).setTextColor(R.id.tv_card_roll_item_time, Color.parseColor("#FFFFFF"));
        helper.setText(R.id.tv_card_roll_item_city, "使用地区：" + item.getCoupon().getAreaNames()).setTextColor(R.id.tv_card_roll_item_city, Color.parseColor("#FFFFFF"));
        if (item.getCoupon().getMin() == 0) {
            helper.setText(R.id.tv_card_roll_rule, "使用规则：不限").setTextColor(R.id.tv_card_roll_rule, Color.parseColor("#FFFFFF"));
        } else {
            helper.setText(R.id.tv_card_roll_rule, "使用规则：满" + item.getCoupon().getMin() + "元可用").setTextColor(R.id.tv_card_roll_rule, Color.parseColor("#FFFFFF"));
        }
    }

    public boolean CardTiem(String StartTiem, String EndTime) {

        //判断是否有开始时间（有表示需要在这个时间段才能使用，没有表示在结束时间都可以用）
        if (TextUtils.isEmpty(StartTiem)) {
            SimpleDateFormat sdfday = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            try {
                Date date = new Date(System.currentTimeMillis());
                String nowTime = sdfday.format(date);
                Date date1 = sdfday.parse(nowTime);
                Date date2 = sdfday.parse(EndTime);//结束时间
                if (date1.getTime() <= date2.getTime()) {
                    return true;
                } else {
                    return false;
                }
            } catch (Exception e) {

            }

        } else {
            try {
                SimpleDateFormat sdfday = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                Date date = new Date(System.currentTimeMillis());
                String nowTime = sdfday.format(date);
                Date date1 = sdfday.parse(nowTime);
                Date date2 = sdfday.parse(StartTiem);
                Date date3 = sdfday.parse(EndTime);
                if (date2.getTime() <= date1.getTime() && date3.getTime() >= date1.getTime()) {
                    return true;
                } else {
                    return false;
                }
            } catch (Exception e) {

            }
        }
        return false;
    }
}
